//
//  CreateJobViewModel.swift
//  GreenDeal
//
//  Created by Moennig Q on 2/3/2023.
//

import UIKit
enum TradeMode: String {
    case credit = "Credit"
    case cash = "Cash"
    case none = ""
}
enum StoryTypeValue: String {
    case SingleStory = "Single story"
    case MultiStory = "Multi story"
    case None = ""
}
enum RoofTypeValue: String {
    case Tile = "Tile"
    case Colorbond = "Colorbond"
    case Other = "Other"
    case None = ""
}
enum PanelTypeValue: String {
    case Mono = "Mono"
    case Poly = "Poly"
    case Both = "Both"
    case None = ""
}
enum PhaseTypeValue: String {
    case Single = "Single"
    case Two = "Two"
    case Three = "Three"
    case None = ""
}
enum MountingTypeValue: String {
    case BuildOnBuiding = "true"
    case NotBuildOnBuiding = "false"
    case None = ""
}
class CreateJobViewModel: NSObject {
    //第一步 company & agent
    @objc dynamic var summaryViewModel = CreateJobSummaryViewModel()
    //第二步 installation Address
    @objc dynamic var installationAddressViewModel = CreateJobInstallAddressViewModel()
    //第三步 site attributes
    @objc dynamic var siteAttributesViewModel = CreateJobSiteAttributesViewModel()
    //第四步 Property owner
    @objc dynamic var propertyOwner = EditOwnerInfomationViewModel()
    //第五步 Schedule date of Install, 默认是今天，从今天往前一年为最小日期，往后不限制
    var installDate: Date = Date()
    //第六步 SystemInfo
    var systemDetail = SystemDetailViewModel()
    //第七步 installer
    var installerDetail = CreateJobInstallerViewModel()
    
}
class CreateJobSummaryViewModel: NSObject {
    var spv_check: Bool = true
    @objc dynamic var company_id: Int = 0
    var company_name: String = ""
    @objc dynamic var agent_id: Int = 0
    var agent_name: String = ""
    var trade_mode: TradeMode = .none {
        didSet{
            self.trade_mode_value = trade_mode.rawValue
        }
    }
    @objc dynamic var trade_mode_value: String = ""
    
    @objc dynamic var customer_po: String = ""
}
class CreateJobSiteAttributesViewModel: NSObject {
    var story: StoryTypeValue = .None {
        didSet{
            self.story_value = story.rawValue
        }
    }
    var install_on_building: MountingTypeValue = .None {
        didSet{
            self.install_on_building_value = install_on_building.rawValue
        }
    }
    var roof: RoofTypeValue = .None
    var phase: PhaseTypeValue = .None
    var panel_type: PanelTypeValue = .None
    //要观察的属性
    @objc dynamic var story_value: String = ""
    @objc dynamic var install_on_building_value: String = ""
}
class CreateJobInstallAddressViewModel: NSObject {
    @objc dynamic var installAddress = PhysicalAddressViewModel()
    var addSpecialAddressFlag: Bool = false
    var specialAddress: String = ""
    @objc dynamic var sameAsPostalAddress: Bool = true
    @objc dynamic var postalAddress = PhysicalAndPOBoxAddressViewModel()
}
struct SystemDetailViewModel {
    var installation_type: InstallationTypeValue = .New//new_system,replacement_system,additional_system,extension_system, 创建默认值是New
    var installation_type_name: String {
        switch installation_type{
        case.New:
            return InstallationTypeName.New.rawValue
        case.Additional:
            return InstallationTypeName.Additional.rawValue
        case.Extension:
            return InstallationTypeName.Extension.rawValue
        case.Replacement:
            return InstallationTypeName.Replacement.rawValue
        default:
            break
        }
        return ""
    }
    var rec_quantity: String = ""
    //获取panel的总功率数(KW)格式化的
    var total_panes_watts_format: String {
        var systemSize: Double = 0
        let panelSizeList = self.panels.map({$0.watts_per_panel * $0.number_panels})
        panelSizeList.forEach({systemSize += Double($0)})
        let systemSizeStr = Tool().formatAmountToStr(amount: "\(systemSize / 1000)", maximumFractionDigits: 3,minimumFractionDigits: 0)
        return systemSizeStr
    }
    //获取panel的总功率数(KW)
    var total_panes_watts: Double {
        var systemSize: Double = 0
        let panelSizeList = self.panels.map({$0.watts_per_panel * $0.number_panels})
        panelSizeList.forEach({systemSize += Double($0)})
        
        return systemSize / 1000
    }
    var panels = [InstallationDetailSystemDetailForPanelModel]()
    var inverters = [InstallationDetailSystemDetailForInverterModel]()
    var batteries = [InstallationDetailSystemDetailForBatteryModel]()
    var connected_type: GridConnectedValue = .offGrid//on_grid, off_grid
    var connected_type_name: String {
        if connected_type == .onGrid {
            return GridConnectedName.onGrid.rawValue
        }else{
            return GridConnectedName.offGrid.rawValue
        }
    }
    var nmi: String = ""
    var gridConnectionApplicationRefNo: String = ""
    var is_battery: Bool = false//是否有电池
    var installed_optimiser: Bool = false//是否安装了优化器
    var additional_capacity_details: String = ""
}

class CreateJobInstallerViewModel: NSObject {
    var installerID: Int = 0
    var designerID: Int = 0
    var electricianID: Int = 0
    var installerName: String = ""
    var designerName: String = ""
    var electricianName: String = ""
    //
    var installer_job_site_instruction: String = ""
    @objc dynamic var pickupRequired: Bool = false
    //
    var address_type: DeliveryWarehouseType = .none 
    //other ，osw共用
    var delivery_address_id: Int = 0
    @objc dynamic var delivery_address_text: String = ""//接口列表中搜索到的
    @objc dynamic var pickupReference: String = ""
    //选择的user id是否有效，因为更改company id的时候，原来选择的user id可能无效
    var installer_valid_flag: Bool = true
    var designer_valid_flag: Bool = true
    var electrician_valid_flag: Bool = true
    var delivery_address_valid_flag: Bool = true
}
